<?php

/***
 * create paging tag
* @author DuongNV
* @copyright Tan MA JSC
* @category common function
* @version 1.1
*/

/**
 * get full url
 */
function curPageURL() {

    $pageURL = 'http';
    // if ($_SERVER["HTTPS"] == "on") {
    // $pageURL .= "s";
    // }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        //$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

/**
 * 
 */
function getPageNumber(){
    
    $curUrl = curPageURL();
    
    if (strpos($curUrl,"page-")){
        $pageNumb = explode("page-", $curUrl);
        $pageNumb = $pageNumb[1];
        return $pageNumb;
    }
    
    return 1;
}

/**
 * 
 * @param unknown_type $pageNumb
 */
function createLinkPaging($pageNumb){
    
    $curUrl = curPageURL();
    
    if (strpos($curUrl,"?page-")){
        $link = explode("?page-", $curUrl);
        $link = $link[0] . "?page-";
    } else if (strpos($curUrl,"&page-")){
        $link = explode("&page-", $curUrl);
        $link = $link[0] . "&page-";
    } else {
        if (strpos($curUrl,"?")){
            $link = $curUrl . "&page-";
        } else {
            $link = $curUrl . "?page-";
        }
    }
    
    $link = str_replace(SITE_NAME, "", $link);
    
    $link .= $pageNumb;
    
    return $link;
}

/**
 * 
 * @param unknown_type $tableName
 * @param unknown_type $conArray
 * @param unknown_type $limit
 * @param unknown_type $type
 * @return string
 */
function createPaging($tableName, $conArray , $limit, $type = null){
    
    
    // count item
    $db = new DBAccess();
    if ($type == null) {
        $count = $db->selectCountRecord( $tableName, $conArray);
    } else {
        $count = $db->selectCountRecordDifficult( $tableName, $conArray);
    }
    
    
    // check page number count
    if ($count < $limit) {
        return "<span class=\"pageView\">( 1 | 1 trang )</span><input type=\"hidden\" name=\"totalPageNumb\" id=\"totalPageNumb\" value=\"1\"/>";
    }
    $count = ceil( $count / $limit);
    if ($count == 1) {
        return "<span class=\"pageView\">( 1 | 1 trang )</span><input type=\"hidden\" name=\"totalPageNumb\" id=\"totalPageNumb\" value=\"1\"/>";
    }
    
    // get current page number
    $curPage = getPageNumber();
    $pageArray = array();
    
    // in case current page number less than 3
    if ($curPage < 3){
        for ($i = 1; $i <= 6 && $i <= $count ; $i++) {
            $pageArray[count($pageArray)] = $i;
        }
    }
    
    
    $back = 1;
    $next = $count;
    if ($curPage > 1){
        $back = $curPage - 1;
    }
    if ($curPage < $count){
        $next = $curPage + 1;
    }
    
    $back = createLinkPaging($back);
    $next = createLinkPaging($next);
    
    $listPage = "<ul class=\"listPage\"><li><a href=\"$back\"><button class=\"back\"></button></a></li>";
    
    $listPage .= "<li><a href=\"$next\"><button class=\"next\"></button></a></li></ul>";
    $listPage .= "<span class=\"pageView\">( $curPage | $count trang )</span><input type=\"hidden\" name=\"totalPageNumb\" id=\"totalPageNumb\" value=\"$count\"/>";
    
    return $listPage;
    
}


/**
 *
 * @param unknown_type $tableName
 * @param unknown_type $conArray
 * @param unknown_type $limit
 * @param unknown_type $type
 * @return number
 */
function createRandomPageNumber($tableName, $conArray , $limit, $type = null){

    // count item
    $db = new DBAccess();
    if ($type == null) {
        $count = $db->selectCountRecord( $tableName, $conArray);
    } else {
        $count = $db->selectCountRecordDifficult( $tableName, $conArray);
    }

    $count = ceil( $count / $limit);
    return rand(1, $count);
}

?>
